package com.julang.check.translate.baidu;


import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.julang.check.utils.CommonData.APP_ID;
import static com.julang.check.utils.CommonData.SECURITY_KEY;

/**
 * @author 赵自强
 * */
public class BaiduTranslator {
    private static final String TRANS_API_HOST = "http://api.fanyi.baidu.com/api/trans/vip/translate";
    public static final MediaType JSON
            = MediaType.get("application/json; charset=utf-8");
    private String appid;
    private String securityKey;

    public BaiduTranslator(String appid, String securityKey) {
        this.appid = appid;
        this.securityKey = securityKey;
    }

    public String getTransResult(String query, String from, String to) {
        System.out.println("from: " + from + "  to:" + to + "  data: " + query);
        Map<String, String> params = buildParams(query, from, to);
        return HttpGet.get(TRANS_API_HOST, params);
    }

    public String getTransResultByPost(String query, String from, String to) throws IOException {
        //创建表单请求体
        FormBody.Builder formBody = new FormBody.Builder();
        formBody.add("q", query);
        formBody.add("from", from);
        formBody.add("to", to);
        formBody.add("appid", appid);
        String salt = String.valueOf(System.currentTimeMillis());
        formBody.add("salt", salt);
        formBody.add("sign", Md5.md5(appid + query + salt + securityKey));
        String s = post(TRANS_API_HOST, formBody.build());
        return s;
    }
    private String post(String url, FormBody json) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .post(json)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
    String post(String url, String json) throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
    private Map<String, String> buildParams(String query, String from, String to) {
        Map<String, String> params = new HashMap<String, String>(7);
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);

        params.put("appid", appid);

        // 随机数
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);

        // 签名  加密前的原文
        String src = appid + query + salt + securityKey;
        params.put("sign", Md5.md5(src));

        return params;
    }

    public static void main(String[] args) throws IOException {
        System.out.println(new BaiduTranslator(APP_ID, SECURITY_KEY).getTransResultByPost("我本将心向明月","zh","en"));
    }
}
